<template>
  <div class="userManagement-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item
            :label="$t('accountManage.tenantAccountNumber') /*租户账号*/"
          >
            <el-input
              v-model.trim="queryForm.account"
              :placeholder="
                $t('accountManage.pleaseEditTenantNumber') /*请输入租户账号*/
              "
              clearable
              disabled
            />
          </el-form-item>
          <el-form-item :label="$t('accountManage.deviceAccount') /*设备账号*/">
            <el-input
              v-model.trim="queryForm.deviceAccount"
              :placeholder="
                $t('accountManage.pleaseEditDeviceAccount') /*请输入设备账号*/
              "
              clearable
            />
          </el-form-item>
          <el-form-item
            :label="$t('accountManage.agentAccountNumber') /*代理商账号*/"
          >
            <el-input
              v-model.trim="queryForm.DailAccount"
              :placeholder="
                $t(
                  'accountManage.pleaseEditAgentAccountNumber'
                ) /*请输入代理商账号*/
              "
              clearable
            />
          </el-form-item>
          <el-form-item :label="$t('accountManage.status') /*状态*/">
            <el-select
              v-model="queryForm.region"
              :placeholder="$t('accountManage.pleaseSelStatus') /*请选择状态*/"
            >
              <el-option
                :label="$t('common.urgentNeed') /*启用*/"
                value="shanghai"
              ></el-option>
              <el-option
                :label="$t('common.disable') /*禁用*/"
                value="beijing"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ $t('common.search') /*查询*/ }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="24">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit('', 'newAccountManagementEdit')"
        >
          {{ $t('common.newBug') /*新购*/ }}
        </el-button>
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit('', 'newAccountManagementEdit')"
        >
          {{ $t('common.renewal') /*续费*/ }}
        </el-button>
        <el-button icon="el-icon-delete" type="danger" @click="handleDelete">
          {{ $t('common.disable') /*禁用*/ }}
        </el-button>
        <el-button icon="el-icon-delete" type="danger" @click="handleDelete">
          {{ $t('common.urgentNeed') /*启用*/ }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      @selection-change="setSelectRows"
    >
      <el-table-column show-overflow-tooltip type="selection"></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="username"
        :label="$t('accountManage.tenantAccountNumber') /*租户账号*/"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="count_dev"
        :label="$t('accountManage.equipmentAccounts') /*设备账号数量*/"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="mark"
        :label="$t('accountManage.remark') /*备注*/"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        :label="$t('accountManage.status') /*状态*/"
      >
        <template #default="{ row }">
          <el-tag>
            {{ setStatusName(row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="fm_end_time"
        :label="$t('accountManage.expirationTime') /*过期时间*/"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="agent_name"
        :label="$t('accountManage.agentAccountNumber') /*代理商账号*/"
      ></el-table-column>
      <!-- <el-table-column
        show-overflow-tooltip
        prop="email"
        label="邮箱"
      ></el-table-column> -->

      <el-table-column
        show-overflow-tooltip
        :label="$t('common.operate') /*操作*/"
        width="300"
      >
        <template #default="{ row }">
          <!-- <el-button type="text" @click="handleEdit(row)">编辑</el-button> -->
          <el-button type="text" @click="handleEdit(row, 'passwordReset')">
            {{ $t('accountManage.passwordReset') /*密码重置*/ }}
          </el-button>
          <el-button
            type="text"
            @click="handleEdit(row, 'deviceManagementEdit')"
          >
            {{ $t('common.deviceSetIndex') /*设备设置*/ }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ $t('accountManage.deviceUnbinging') /*设备解绑*/ }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :page-size="queryForm.limit"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
    <device-management-edit
      ref="deviceManagementEdit"
      @fetch-data="fetchData"
    />
    <new-account-management-edit
      ref="newAccountManagementEdit"
      @fetch-data="fetchData"
    />
    <password-reset ref="passwordReset" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getDevAccountList } from '@/api/agents.js'
  import { doDelete } from '@/api/userManagement'
  import NewAccountManagementEdit from '@/views/accountManagement/userManagement/components/newAccountManagementEdit.vue'
  import PasswordReset from '@/views/accountManagement/userManagement/components/passwordReset.vue'
  import DeviceManagementEdit from './components/DeviceManagementEdit'
  export default {
    name: 'UserManagement',
    components: {
      DeviceManagementEdit,
      NewAccountManagementEdit,
      PasswordReset,
    },
    props: {
      agentUserInfo: {
        default: function () {
          return {}
        },
        type: Object,
      },
    },
    data() {
      return {
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          page: 1,
          limit: 10,
          tenant_user_id: this.agentUserInfo?.user_id || '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      setStatusName(status) {
        let statusName = '启用'
        if (status === 1) {
          statusName = '禁用'
        } else if (status === 2) {
          statusName = '过期'
        }
        return statusName
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row, type = 'edit') {
        if (row.id) {
          this.$refs[type].showEdit(row)
        } else {
          this.$refs[type].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            this.$baseMessage(msg, 'success')
            this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              this.$baseMessage(msg, 'success')
              this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error')
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.limit = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.page = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        // const { data, totalCount } = userData || (await getList(this.queryForm))
        console.log(this.queryForm, '设备管理请求参数')
        if (!this.queryForm?.tenant_user_id) return
        const { data } = await getDevAccountList(this.queryForm)
        console.log(data, '设备账号管理')
        this.list = data?.data
        this.total = data?.count || 0
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
